package cn.cvs.controller;

import cn.cvs.pojo.TSysUser;
import cn.cvs.service.SysUserService;
import cn.cvs.utils.Constants;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user")
public class LoginController {

    @Resource
    SysUserService sysUserService;
    //去登录页面
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "/login.jsp";
    }

    //登录页面提交用户名密码,之后进行验证
    @RequestMapping(path = "/login")
    public String login( HttpServletRequest request
                        ,@RequestParam String account
                        ,@RequestParam String password){
        TSysUser user = sysUserService.login(account,password);
        if(null != user){
            request.getSession().setAttribute(Constants.userSession,user);
            //登录成功
            return "redirect:/user/toMain"; //重定向
        }
        return "/login.jsp";
    }

    //去主页
    @RequestMapping(value = "/toMain")
    public String toMain(){
        //获取到userlist
        return "/frame.jsp";
    }

    @RequestMapping("/doLogout")
    public String doLogout(HttpServletRequest request){
//        request.getSession().removeAttribute("userSession");
        request.getSession().removeAttribute(Constants.userSession);
        return "/login.jsp";
    }
}
